package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;

import com.xiyoumobile.dengtuo.flowershop.R;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import adapter.ImageAdapter;
import common.ImageHandler;
import javahtml5.JsMainActivity;

/**
 * Created by Admin on 2016/3/8.
 */
public class HomeFragment extends Fragment{




    public ViewPager viewPager;
    public ImageHandler handler = null;

    private WebView webView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_index,null);
        findView(view);
        return view;
    }




    private void findView(View view) {


        webView = (WebView) view.findViewById(R.id.webView);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        init();


        // WebView打开权限
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);

        webView.setWebViewClient(new WebViewClient());
        JsMainActivity javaSrcipt =
                new JsMainActivity(getContext(),webView);
        webView.addJavascriptInterface(javaSrcipt, "native");

        webView.loadUrl("file:///android_asset/index.html");


    }

    public void init() {

        ImageView v1 = new ImageView(this.getContext());
        v1.setScaleType(ImageView.ScaleType.FIT_XY);
        v1.setImageResource(R.mipmap.imageo);
        ImageView v2 = new ImageView(this.getContext());
        v2.setScaleType(ImageView.ScaleType.FIT_XY);
        v2.setImageResource(R.mipmap.imaget);
        ImageView v3 = new ImageView(this.getContext());
        v3.setScaleType(ImageView.ScaleType.FIT_XY);
        v3.setImageResource(R.mipmap.imageth);
        ImageView v4 = new ImageView(this.getContext());
        v4.setScaleType(ImageView.ScaleType.FIT_XY);
        v4.setImageResource(R.mipmap.imagef);
        ArrayList<ImageView> views = new ArrayList<>();
        views.add(v1);
        views.add(v2);
        views.add(v3);
        views.add(v4);
        ImageAdapter imageAdapter = new ImageAdapter(views);
        viewPager.setAdapter(imageAdapter);
        //开始轮播效果
        handler = new ImageHandler(new WeakReference<>(this),views.size());
        handler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY);
    }
}
